
Yacon.widget.grid.list.BaseRow = function (conText)
{
	this.fieldNames       =  null;
	this.fieldList        =  null;
	this.activeField      =  null;
	
	Yacon.apply(this,conText);
}
Yacon.widget.grid.list.BaseRow.Extend(Yacon.widget.WinControl,{
	
	addField:function(field)
	{
	
		var fieldcount = this.fieldNames.length;
	
		this.fieldNames[fieldcount] = field.getFieldName().toLowerCase();
		this.fieldList[fieldcount] = field;	
		this.activeField = field;
	},
	getField:function(fieldIndex)
	{
		if (!this.fieldNames||!this.fieldList)
			return null;
		
		var field = null;
		if (typeof(fieldIndex) == "number")
		{
	     	field = this.fieldList[fieldIndex];
	    }else  if (typeof(fieldIndex) == "string")
		{
	    	var index = this.fieldNames.indexOf(fieldIndex.toLowerCase());
	       	if (index >-1)
			{
				field= this.fieldList[index];
			}
	    	
		}
	    this.activeField = field;
	    
		return field;
	},
	getFieldCount:function()
	{
		return this.fieldList.length;
	}
});

Yacon.widget.grid.list.DataRow = function (conText)
{
	this.activeCell = null;	
	
	Yacon.apply(this,conText);
	
	this.create();
}
Yacon.widget.grid.list.DataRow.Extend(Yacon.widget.grid.list.BaseRow,{
		
	create:function()
	{
		this.fieldNames       =  new Array();
		this.fieldList        =  new Array();
		
		if (this.element)
			this.element.Control = this;
		
		this.ifConver = false;
	},
	setElement:function(element)
	{
		this.element = element;
		if (this.element)
			this.element.Control = this;
		
	},
	isCheck:function()
	{
		var checkValue = false;
		if (this.parentList.isCheck()&& this.element.cells[0]&& this.element.cells[0].children[0])
			checkValue = this.element.cells[0].children[0].checked;
		
		return checkValue;
	},
	getRecord:function()
	{
		return this.record;
	},	
	setWidth:function(width)
	{
		this.setStyle("width",width);
	},	
	getWidth:function()
	{
		if(this.element)
			return this.element.offsetWidth;
		else
			return 0;
	},
	changeFieldWidth:function()
	{
		for(var i=0;i< this.fieldList.length;i++)
		{
			this.fieldList[i].changeFieldWidth();
		}
	},
	setHeight:function(height)
	{
		this.setStyle("height",height);
	},	
	getHeight:function()
	{
		if(this.element)
			return this.element.offsetHeight;
		else
			return 0;
	},
	setRowIndex:function(rowIndex)
	{
		this.rowIndex = rowIndex;
	},	
	setActiveCell:function(cellObj)
	{
		if (cellObj == undefined)
			return;
		
		this.parent.setActiveRow(this.element);
				
		if(this.activeCell != undefined)
			this.activeCell.className = "";
				
		this.activeCell = cellObj;
		
		if (this.activeCell.Control&&this.activeCell.Control.activeCell)
			this.activeCell.Control.activeCell();
	},
	setFieldValue:function(fieldName,fieldValue,fieldText)
	{
		
		this.activeField = this.getField(fieldName);
		if (this.activeField && this.parentList.isEdit())
			this.activeField.setCellValue(fieldValue,fieldText);
	},
	convertRecord:function()
	{
		var dataCell,headCell;
		
		if (!this.ifConver)
		{
			if (this.getFieldCount()==0)
			{
				for (var j=0;j<this.element.cells.length;j++)
				{
					headCell = this.parentList.headControl.getField(j);
					if (!this.parentList.isEdit())
						dataCell = new Yacon.widget.grid.list.DataCell({headCell:headCell,element:this.element.cells[j]});
					else
						dataCell = new Yacon.widget.grid.list.EditCell({headCell:headCell,element:this.element.cells[j]});
					
					dataCell.parent     = this;
					dataCell.parentList = this.parentList;
					
					this.addField(dataCell);
				}
			}else
			{
				/*	
				for (var j=0;j<this.getFieldCount();j++)
				{
					var filed =this.getField(j);
					this.getField(j).setElement(this.element.cells[j]);
				}*/
			}
			this.ifConver =true;
		}
		if (this.record.getFieldCount()==0)
		{
			for (var j=0;j<this.getFieldCount();j++)
			{
				dataCell = this.getField(j);
				if (!Yacon.isEmpty(dataCell.getFieldName()))
					this.record.setFieldValue(dataCell.getFieldName(),dataCell.getFieldValue(),dataCell.getFieldText());				
			}
		}
	},
	broadRecord:function()
	{
		for (var j=0;j<this.getFieldCount();j++)
		{
			dataCell = this.getField(j);	
			if (!Yacon.isEmpty(dataCell.getFieldName()))
				dataCell.setCellValue(this.record.getFieldValue(dataCell.getFieldName()),this.record.getFieldText(dataCell.getFieldName()));				
		}
	},
	creatElement:function()
	{
		var activeIndex = 0;
		if ( !this.parentList.isEdit())
		{
			alert("数据列表非编辑状态！");
			return;
		}
		
		if (this.parentList.getActiveRow())
			activeIndex = this.parentList.getActiveRow().rowIndex + 1;
		
			
		this.element         = this.parent.element.insertRow(activeIndex);		
		this.element.Control = this;
		
		var dataCell;
		for(var i=0;i<this.parentList.headControl.getFieldCount();i++)
		{
			
			dataCell = new Yacon.widget.grid.list.EditCell({headCell:this.parentList.headControl.getField(i)});
			dataCell.parent = this;
			dataCell.parentList = this.parentList;
			dataCell.createCell();
			this.addField(dataCell);
		}
		this.addEvents("click",Yacon.widget.grid.list.DataRow.events.onRowClick);
		this.addEvents("dblclick",Yacon.widget.grid.list.DataRow.events.onRowDblClick);
		this.parent.setActiveRow(this.element);
		this.broadDetailPage();
		
	},
	
	creatElementTemplate:function(rowIndx)
	{
		this.setRowIndex(rowIndx);
		
		var rowTem = "<tr onclick='Yacon.widget.grid.list.DataRow.events.onRowClick()' ondblclick='Yacon.widget.grid.list.DataRow.events.onRowDblClick()' ";		
		if ((this.rowIndex % 2) == 0)
			rowTem = rowTem + " class='"+this.parent.evenRowClass+"'";
		else				
			rowTem = rowTem + " class='"+this.parent.oddRowClass+"'";
		
		
		rowTem = rowTem + " >";	
		
		var dataCell,headCell;
		for(var i=0;i< this.parentList.headControl.getFieldCount();i++)
		{
			headCell =this.parentList.headControl.getField(i);
			
			if (!this.parentList.isEdit())
				dataCell = new Yacon.widget.grid.list.DataCell({headCell:headCell});
			else
				dataCell = new Yacon.widget.grid.list.EditCell({headCell:headCell});	
			
			dataCell.parent = this;
			dataCell.parentList = this.parentList;
			dataCell.setCellIndex(i);			
			rowTem = rowTem + dataCell.createTemplate();
			
			this.addField(dataCell);
		}
		rowTem = rowTem + "</tr>";	
		
		return rowTem;
	},
	broadDetailPage:function ()
	{
		if (this.parentList.detailPage)
		{
			var param = "";
			this.parentList.detailPage.beforeFirst();
			var fuhao ="?";
			if (this.parentList.detailPage.getAttrValue("pagePath").contains("?"))
				fuhao ="&";
				
			while(this.parentList.detailPage.next())
			{				
				var filename    = this.parentList.detailPage.getFieldValue("paraName");
				var foreignName = this.parentList.detailPage.getFieldValue("foreignName");
				
				
					
				if (!Yacon.isEmpty(filename)&& !Yacon.isEmpty(foreignName))
				{
					
					if(!Yacon.isEmpty(this.parentList.getDataSet().getFieldValue(foreignName))){
						if (param.isEqual("")){
							param = fuhao +filename +"=" + this.parentList.getDataSet().getFieldValue(foreignName).encode();
						}else{
							param = param +"&" + filename +"=" + this.parentList.getDataSet().getFieldValue(foreignName).encode();
						}
					}
								
				}
			}
			
			if (param.isEqual(""))
				param = fuhao + "state=" + this.parentList.getDataSet().getState();
			else
				param = param +"&state=" + this.parentList.getDataSet().getState();

			//添加固定场景标示
			param += Yacon.getCommonParam();
			
			if (Yacon.isTrue(this.parentList.detailPage.getAttrValue("isuseUpData")))
			{
				Yacon.showModal({
					width:this.parentList.detailPage.getAttrValue("pageWidth"),
					height:this.parentList.detailPage.getAttrValue("pageHeight"),
					dataSet:this.parentList.getDataSet(),
					list:this.parentList,
					url:this.parentList.detailPage.getAttrValue("pagePath")+ param
				});	
				if(window.windowType == "0"){
					this.parentList.refresh();
				}				
			}else
			{
				Yacon.showModal({
					width:this.parentList.detailPage.getAttrValue("pageWidth"),
					height:this.parentList.detailPage.getAttrValue("pageHeight"),
					url:this.parentList.detailPage.getAttrValue("pagePath")+ param,
					list:this.parentList
				});
				if(window.windowType == "0"){
					this.parentList.refresh();
				}			
			}
		}
	},
	rowDblClick:function ()
	{		
		if (this.parentList.isCheck())
		{
			if (this.element.cells[0] && this.element.cells[0].children[0])
			{
				if (this.element.cells[0].children[0].checked)
					this.element.cells[0].children[0].checked = false;
				else
					this.element.cells[0].children[0].checked = true;
			}
		}		
	
		this.broadDetailPage();
			
	},
	
	free:function()
	{
		for (var j=0;j<this.getFieldCount();j++)
		{
			dataCell = this.getField(j);	
			dataCell = null;
			delete dataCell;
		}
		this.fieldNames = null;
		this.fieldList  =   null;
	}
	
});

Yacon.widget.grid.list.DataRow.events = {
	onRowClick:function()
	{
		var rowObj   =  Yacon.getTR(event.srcElement);	
			
		if (!rowObj.Control)
			return;		
		
		rowObj.Control.parentList.broadCastDetail();
		
		
		if (Yacon.isfireuserEvent(rowObj.Control.parentList.elementID+"_onRowClick"))
			Yacon.fireUserEvent(rowObj.Control.parentList.elementID+"_onRowClick",[rowObj.Control.parentList]);
	},
	onRowDblClick:function()
	{
		var rowObj   =  Yacon.getTR(event.srcElement);	
			
		if (!rowObj.Control)
			return;		
		
		
		rowObj.Control.rowDblClick();
		
		if (Yacon.isfireuserEvent(rowObj.Control.parentList.elementID+"_onRowDblClick"))
			Yacon.fireUserEvent(rowObj.Control.parentList.elementID+"_onRowDblClick",[rowObj.Control.parentList]);
	}
}





